/*
 * RDF_Application_2View.java
 */
package rdf_application_2;
//import com.hp.hpl.jena.graph.query.Query;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.core.ResultBinding;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import sparql.RDFParser;
import sparql.RDF_Lexer;

/**
 * The application's main frame.
 */
public class RDF_Application_2View extends FrameView {

    public File rdfFile;
    public File rdfQuery;

    public RDF_Application_2View(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = RDF_Application_2.getApplication().getMainFrame();
            aboutBox = new RDF_Application_2AboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        RDF_Application_2.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultOutput = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        inputQuery = new javax.swing.JTextArea();
        runQuery = new javax.swing.JButton();
        loadRDF = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        inputRDF = new javax.swing.JTextArea();
        loadQueryButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        tokenTxtArea = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        getAllPaperByAuthor = new javax.swing.JCheckBox();
        getAllCitationsOf = new javax.swing.JCheckBox();
        getAllPapersPublishedAt = new javax.swing.JCheckBox();
        authorName = new javax.swing.JTextField();
        paperTitle = new javax.swing.JTextField();
        year = new javax.swing.JTextField();
        getPapersThatHaveCitation = new javax.swing.JCheckBox();
        citationTitle = new javax.swing.JTextField();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        callCalcMenu = new javax.swing.JMenuItem();
        defaultRDF = new javax.swing.JMenuItem();
        openRDF = new javax.swing.JMenuItem();
        loadQueryMenu = new javax.swing.JMenuItem();
        runQueryMenu = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        fileChooser = new javax.swing.JFileChooser();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(900, 700));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        resultOutput.setColumns(20);
        resultOutput.setEditable(false);
        resultOutput.setRows(5);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(rdf_application_2.RDF_Application_2.class).getContext().getResourceMap(RDF_Application_2View.class);
        resultOutput.setToolTipText(resourceMap.getString("resultOutput.toolTipText")); // NOI18N
        resultOutput.setName("resultOutput"); // NOI18N
        jScrollPane1.setViewportView(resultOutput);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        inputQuery.setColumns(20);
        inputQuery.setRows(5);
        inputQuery.setToolTipText(resourceMap.getString("inputQuery.toolTipText")); // NOI18N
        inputQuery.setName("inputQuery"); // NOI18N
        jScrollPane2.setViewportView(inputQuery);

        runQuery.setText(resourceMap.getString("runQuery.text")); // NOI18N
        runQuery.setToolTipText(resourceMap.getString("runQuery.toolTipText")); // NOI18N
        runQuery.setEnabled(false);
        runQuery.setName("runQuery"); // NOI18N
        runQuery.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runQueryMenuActionPerformed(evt);
            }
        });

        loadRDF.setText(resourceMap.getString("loadRDF.text")); // NOI18N
        loadRDF.setToolTipText(resourceMap.getString("loadRDF.toolTipText")); // NOI18N
        loadRDF.setName("loadRDF"); // NOI18N
        loadRDF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openRDFActionPerformed(evt);
            }
        });

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        inputRDF.setColumns(20);
        inputRDF.setRows(5);
        inputRDF.setToolTipText(resourceMap.getString("inputRDF.toolTipText")); // NOI18N
        inputRDF.setEnabled(false);
        inputRDF.setName("inputRDF"); // NOI18N
        jScrollPane3.setViewportView(inputRDF);

        loadQueryButton.setText(resourceMap.getString("loadQueryButton.text")); // NOI18N
        loadQueryButton.setToolTipText(resourceMap.getString("loadQueryButton.toolTipText")); // NOI18N
        loadQueryButton.setEnabled(false);
        loadQueryButton.setName("loadQueryButton"); // NOI18N
        loadQueryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadQueryMenuActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setToolTipText(resourceMap.getString("jButton1.toolTipText")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        tokenTxtArea.setColumns(20);
        tokenTxtArea.setRows(5);
        tokenTxtArea.setToolTipText(resourceMap.getString("tokenTxtArea.toolTipText")); // NOI18N
        tokenTxtArea.setName("tokenTxtArea"); // NOI18N
        jScrollPane4.setViewportView(tokenTxtArea);

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        getAllPaperByAuthor.setText(resourceMap.getString("getAllPaperByAuthor.text")); // NOI18N
        getAllPaperByAuthor.setToolTipText(resourceMap.getString("getAllPaperByAuthor.toolTipText")); // NOI18N
        getAllPaperByAuthor.setEnabled(false);
        getAllPaperByAuthor.setName("getAllPaperByAuthor"); // NOI18N

        getAllCitationsOf.setText(resourceMap.getString("getAllCitationsOf.text")); // NOI18N
        getAllCitationsOf.setToolTipText(resourceMap.getString("getAllCitationsOf.toolTipText")); // NOI18N
        getAllCitationsOf.setEnabled(false);
        getAllCitationsOf.setName("getAllCitationsOf"); // NOI18N

        getAllPapersPublishedAt.setText(resourceMap.getString("getAllPapersPublishedAt.text")); // NOI18N
        getAllPapersPublishedAt.setToolTipText(resourceMap.getString("getAllPapersPublishedAt.toolTipText")); // NOI18N
        getAllPapersPublishedAt.setEnabled(false);
        getAllPapersPublishedAt.setName("getAllPapersPublishedAt"); // NOI18N

        authorName.setText(resourceMap.getString("authorName.text")); // NOI18N
        authorName.setToolTipText(resourceMap.getString("authorName.toolTipText")); // NOI18N
        authorName.setEnabled(false);
        authorName.setName("authorName"); // NOI18N

        paperTitle.setText(resourceMap.getString("paperTitle.text")); // NOI18N
        paperTitle.setToolTipText(resourceMap.getString("paperTitle.toolTipText")); // NOI18N
        paperTitle.setEnabled(false);
        paperTitle.setName("paperTitle"); // NOI18N

        year.setText(resourceMap.getString("year.text")); // NOI18N
        year.setToolTipText(resourceMap.getString("year.toolTipText")); // NOI18N
        year.setEnabled(false);
        year.setName("year"); // NOI18N

        getPapersThatHaveCitation.setText(resourceMap.getString("getPapersThatHaveCitation.text")); // NOI18N
        getPapersThatHaveCitation.setToolTipText(resourceMap.getString("getPapersThatHaveCitation.toolTipText")); // NOI18N
        getPapersThatHaveCitation.setEnabled(false);
        getPapersThatHaveCitation.setName("getPapersThatHaveCitation"); // NOI18N

        citationTitle.setText(resourceMap.getString("citationTitle.text")); // NOI18N
        citationTitle.setToolTipText(resourceMap.getString("citationTitle.toolTipText")); // NOI18N
        citationTitle.setEnabled(false);
        citationTitle.setName("citationTitle"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE))
                        .addGap(19, 19, 19)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 468, Short.MAX_VALUE)
                                .addComponent(jButton1))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 609, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(loadRDF, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(loadQueryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(runQuery, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(86, 86, 86)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(getAllPapersPublishedAt)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(year, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(getAllPaperByAuthor)
                                    .addComponent(getAllCitationsOf))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(authorName, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                                    .addComponent(paperTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)))
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(getPapersThatHaveCitation)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(citationTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)))
                        .addGap(73, 73, 73))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 839, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 839, Short.MAX_VALUE)
                    .addComponent(jLabel2))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(getAllPaperByAuthor)
                                    .addComponent(authorName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(getAllCitationsOf)
                                    .addComponent(paperTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(3, 3, 3)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(getAllPapersPublishedAt)
                                    .addComponent(year, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(getPapersThatHaveCitation)
                                    .addComponent(citationTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(10, 10, 10))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(loadRDF, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(loadQueryButton, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                                        .addComponent(runQuery, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)))
                                .addGap(72, 72, 72)))
                        .addGap(15, 15, 15)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE))
                        .addGap(15, 15, 15))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(635, 635, 635))))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N
        fileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuActionPerformed(evt);
            }
        });

        callCalcMenu.setText(resourceMap.getString("callCalcMenu.text")); // NOI18N
        callCalcMenu.setToolTipText(resourceMap.getString("callCalcMenu.toolTipText")); // NOI18N
        callCalcMenu.setName("callCalcMenu"); // NOI18N
        callCalcMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                callCalcMenuActionPerformed(evt);
            }
        });
        fileMenu.add(callCalcMenu);

        defaultRDF.setIcon(resourceMap.getIcon("defaultRDF.icon")); // NOI18N
        defaultRDF.setText(resourceMap.getString("defaultRDF.text")); // NOI18N
        defaultRDF.setToolTipText(resourceMap.getString("defaultRDF.toolTipText")); // NOI18N
        defaultRDF.setName("defaultRDF"); // NOI18N
        defaultRDF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                defaultRDFActionPerformed(evt);
            }
        });
        fileMenu.add(defaultRDF);

        openRDF.setText(resourceMap.getString("openRDF.text")); // NOI18N
        openRDF.setToolTipText(resourceMap.getString("openRDF.toolTipText")); // NOI18N
        openRDF.setName("openRDF"); // NOI18N
        openRDF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openRDFActionPerformed(evt);
            }
        });
        fileMenu.add(openRDF);

        loadQueryMenu.setText(resourceMap.getString("loadQueryMenu.text")); // NOI18N
        loadQueryMenu.setToolTipText(resourceMap.getString("loadQueryMenu.toolTipText")); // NOI18N
        loadQueryMenu.setName("loadQueryMenu"); // NOI18N
        loadQueryMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadQueryMenuActionPerformed(evt);
            }
        });
        fileMenu.add(loadQueryMenu);

        runQueryMenu.setText(resourceMap.getString("runQueryMenu.text")); // NOI18N
        runQueryMenu.setToolTipText(resourceMap.getString("runQueryMenu.toolTipText")); // NOI18N
        runQueryMenu.setName("runQueryMenu"); // NOI18N
        runQueryMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runQueryMenuActionPerformed(evt);
            }
        });
        fileMenu.add(runQueryMenu);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(rdf_application_2.RDF_Application_2.class).getContext().getActionMap(RDF_Application_2View.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setToolTipText(resourceMap.getString("aboutMenuItem.toolTipText")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 859, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 839, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addContainerGap(703, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(statusMessageLabel)
                        .addComponent(statusAnimationLabel))
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        fileChooser.setDialogTitle(resourceMap.getString("fileChooser.dialogTitle")); // NOI18N
        fileChooser.setName("fileChooser"); // NOI18N

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void openRDFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openRDFActionPerformed
        // TODO add your handling code here:
        int returnVal = fileChooser.showOpenDialog(this.getFrame());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            rdfFile = fileChooser.getSelectedFile();
            try {
                // What to do with the file, e.g. display it in a TextArea
                inputRDF.read(new FileReader(rdfFile.getAbsolutePath()), null);
                enableComponents();
            } catch (IOException ex) {
                System.out.println("problem accessing file" + rdfFile.getAbsolutePath());
            }
        } else {
            System.out.println("File access cancelled by user.");
        }
    }//GEN-LAST:event_openRDFActionPerformed

    private void loadQueryMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadQueryMenuActionPerformed
        // TODO add your handling code here:

        int returnVal = fileChooser.showOpenDialog(this.getFrame());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            rdfQuery = fileChooser.getSelectedFile();
            try {
                // What to do with the file, e.g. display it in a TextArea
                inputQuery.read(new FileReader(rdfQuery.getAbsolutePath()), null);
                enableComponents();
            } catch (IOException ex) {
                System.out.println("problem accessing file" + rdfQuery.getAbsolutePath());
            }
        } else {
            System.out.println("File access cancelled by user.");
        }
    }//GEN-LAST:event_loadQueryMenuActionPerformed

    private void runQueryMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runQueryMenuActionPerformed
        //TODO add your handling code here:
        try {

            // Check for selected checkbox
            // Add Extra Triples
            StringBuilder buildedQuery = new StringBuilder();
            StringBuilder extraTriples = new StringBuilder();
            if (getAllCitationsOf.isSelected() || getAllPaperByAuthor.isSelected() 
                    || getAllPapersPublishedAt.isSelected()
                    || getPapersThatHaveCitation.isSelected()) {
                ResourceBundle bundle = ResourceBundle.getBundle(
                        "rdf_application_2.template", Locale.ENGLISH);
                buildedQuery.append("def: ");
                buildedQuery.append(bundle.getString("def_article"));
                buildedQuery.append(" ");
                buildedQuery.append(bundle.getString("def_url"));
                buildedQuery.append(";");
                buildedQuery.append("\n");

                StringBuilder variables = new StringBuilder("var: ;");
                if (getAllCitationsOf.isSelected()) {
                    variables.insert(variables.lastIndexOf(";"), " " + bundle.getString("def_citation_title"));
                    extraTriples.append(bundle.getString("def_citation_triple"));
                    extraTriples.append("\n");
                    extraTriples.append(bundle.getString("def_paper_title_triple"));
                    extraTriples.append(" ");
                    extraTriples.append("\"");
                    extraTriples.append(paperTitle.getText().trim());
                    extraTriples.append("\";");
                    extraTriples.append("\n");
                    extraTriples.append(bundle.getString("def_citation_title_triple"));
                    extraTriples.append(" ");
                    extraTriples.append(bundle.getString("def_citation_title"));
                    extraTriples.append(";");
                    extraTriples.append("\n");
                }
                if (getAllPaperByAuthor.isSelected()) {
                    variables.insert(variables.lastIndexOf(";"), " " + bundle.getString("def_title"));
                    variables.insert(variables.lastIndexOf(";"), " " + bundle.getString("def_year"));
                    extraTriples.append(bundle.getString("def_author_triple"));
                    extraTriples.append("\n");
                    extraTriples.append(bundle.getString("def_author_name_triple"));
                    extraTriples.append(" ");
                    extraTriples.append("\"");
                    extraTriples.append(authorName.getText().trim());
                    extraTriples.append("\";");
                    extraTriples.append("\n");
                    extraTriples.append(bundle.getString("def_publishedyear_triple"));
                    extraTriples.append(" ");
                    extraTriples.append(bundle.getString("def_year"));
                    extraTriples.append(";");
                    extraTriples.append("\n");
                }

                if (getAllPapersPublishedAt.isSelected()) {
                    variables.insert(variables.lastIndexOf(";"), " " + bundle.getString("def_title"));
                    extraTriples.append(bundle.getString("def_publishedyear_triple"));
                    extraTriples.append(" ");
                    extraTriples.append("\"");
                    extraTriples.append(year.getText().trim());
                    extraTriples.append("\"");
                    extraTriples.append(";");
                    extraTriples.append("\n");
                }

                if (getPapersThatHaveCitation.isSelected()){
                    variables.insert(variables.lastIndexOf(";"), " " + bundle.getString("def_title"));
                    extraTriples.append(bundle.getString("def_citation_title_triple"));
                    extraTriples.append(" ");
                    extraTriples.append("\"");
                    extraTriples.append(citationTitle.getText().trim());
                    extraTriples.append("\"");
                    extraTriples.append(";");
                    extraTriples.append("\n");
                    extraTriples.append(bundle.getString("def_citation_triple"));
                    extraTriples.append("\n");

                }
                extraTriples.append(bundle.getString("def_paper_title_triple"));
                extraTriples.append(" ");
                extraTriples.append(bundle.getString("def_title"));
                extraTriples.append(";");
                extraTriples.append("\n");

                buildedQuery.append(variables);
                buildedQuery.append("\n");
                buildedQuery.append(extraTriples);

                //Set into inputQuery
                inputQuery.setText(buildedQuery.toString());
            }

            String inputedQuery = inputQuery.getText();

            InputStream is = new ByteArrayInputStream(inputedQuery.getBytes("UTF-8"));
            RDF_Lexer lexer = new RDF_Lexer(is);
            RDFParser rdfparser = new RDFParser(lexer);
            rdfparser.parse();
            rdfparser.check_variable();
            resultOutput.append("\n------ Translated Query -------\n" + rdfparser.translatedQuery.toString());
            System.out.print(rdfparser.translatedQuery.toString());

            Model articleModel = ModelFactory.createDefaultModel();
            articleModel.read(new FileReader(rdfFile.getAbsolutePath()), "");
            String queryString = rdfparser.translatedQuery.toString();
            Query query = QueryFactory.create(queryString);
            QueryExecution queryExecution = QueryExecutionFactory.create(query, articleModel);
            ResultSet resultSet = queryExecution.execSelect();
            ResultSet resultSet2 = resultSet;
            int counter = 0;
            ArrayList<String> var_used = new ArrayList<String>();
            var_used = (ArrayList<String>) resultSet.getResultVars();
            String[] columnNames = new String[var_used.size()];
            Object[][] data = new Object[20][var_used.size()];
            for (int i = 0; i < var_used.size(); i++) {
                columnNames[i] = var_used.get(i);
            }

            for (Iterator iter2 = resultSet2; iter2.hasNext();) {
                ResultBinding res = (ResultBinding) iter2.next();

                counter = doAddition(counter, 1);
                for (int i = 0; i < var_used.size(); i++) {
                    Object abc = res.get(var_used.get(i));
                    data[counter][i] = abc.toString();

                }
            }

            SimpleTableDemo.rowCount = String.valueOf(counter);
            SimpleTableDemo.columnNames = columnNames;
            SimpleTableDemo.data = data;
            SimpleTableDemo.createAndShowGUI();

        } catch (Exception ex) {
            resultOutput.append(ex.getMessage());
            System.out.println(ex.getMessage());
        }
    }//GEN-LAST:event_runQueryMenuActionPerformed

    private int doAddition(int op1, int op2){
        calculator_gui cg = new calculator_gui();
        return cg.doAddition(op1, op2);
    }
    private void callCalcMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_callCalcMenuActionPerformed
        // TODO add your handling code here:
        calculator_gui.run_calc_gui();
    }//GEN-LAST:event_callCalcMenuActionPerformed

    private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileMenuActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fileMenuActionPerformed

    private void defaultRDFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defaultRDFActionPerformed
        //int returnVal = fileChooser.showOpenDialog(this.getFrame());
        //if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            rdfFile = new File("RDF_Files_test/Article.rdf");
            try {
                // What to do with the file, e.g. display it in a TextArea
                inputRDF.read(new FileReader(rdfFile.getAbsolutePath()), null);
                inputQuery.read(new FileReader((new File("RDF_Files_test/article-query.txt")).getAbsolutePath()), null);
                // Enabling Components
                enableComponents();
            } catch (IOException ex) {
                System.out.println("problem accessing file" + rdfFile.getAbsolutePath());
            }
        }
    }//GEN-LAST:event_defaultRDFActionPerformed

    private void enableComponents() {
        loadQueryButton.setEnabled(true);
        loadQueryMenu.setEnabled(true);
        runQuery.setEnabled(true);
        runQueryMenu.setEnabled(true);
        getAllCitationsOf.setEnabled(true);
        getAllPaperByAuthor.setEnabled(true);
        getAllPapersPublishedAt.setEnabled(true);
        getPapersThatHaveCitation.setEnabled(true);
        authorName.setEnabled(true);
        paperTitle.setEnabled(true);
        year.setEnabled(true);
        citationTitle.setEnabled(true);
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        resultOutput.setText("");
        tokenTxtArea.setText("");
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField authorName;
    private javax.swing.JMenuItem callCalcMenu;
    private javax.swing.JTextField citationTitle;
    private javax.swing.JMenuItem defaultRDF;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JCheckBox getAllCitationsOf;
    private javax.swing.JCheckBox getAllPaperByAuthor;
    private javax.swing.JCheckBox getAllPapersPublishedAt;
    private javax.swing.JCheckBox getPapersThatHaveCitation;
    private javax.swing.JTextArea inputQuery;
    private javax.swing.JTextArea inputRDF;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JButton loadQueryButton;
    private javax.swing.JMenuItem loadQueryMenu;
    private javax.swing.JButton loadRDF;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openRDF;
    private javax.swing.JTextField paperTitle;
    private javax.swing.JProgressBar progressBar;
    public static javax.swing.JTextArea resultOutput;
    private javax.swing.JButton runQuery;
    private javax.swing.JMenuItem runQueryMenu;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    public static javax.swing.JTextArea tokenTxtArea;
    private javax.swing.JTextField year;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
}
